home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / QuArK / plugins / map2grid.py < prev    next >
Text File  |  2004-01-05  |  6KB  |  202 lines

  1. """   QuArK  -  Quake Army Knife
  2.  
  3. A tricky "force-the-whole-map-polyhedrons-to-grid"
  4. """
  5. #
  6. # Copyright (C) 1996-99 Armin Rigo
  7. # THIS FILE IS PROTECTED BY THE GNU GENERAL PUBLIC LICENCE
  8. # FOUND IN FILE "COPYING.TXT"
  9. #
  10.  
  11. #$Header: /cvsroot/quark/runtime/plugins/map2grid.py,v 1.5 2003/09/14 05:57:08 cdunde Exp $
  12.  
  13.  
  14.  
  15. Info = {
  16.    "plug-in":       "Force All Polyhedrons to grid",
  17.    "desc":          'A tricky "force-the-whole-map-polyhedrons-to-grid".',
  18.    "date":          "11 feb 99",
  19.    "author":        "Armin Rigo",
  20.    "author e-mail": "arigo@planetquake.com",
  21.    "quark":         "Version 5.6" }
  22.  
  23.  
  24. import quarkx
  25. from quarkpy.maputils import *
  26. import quarkpy.qmenu
  27. import quarkpy.mapcommands
  28. import quarkpy.qmacro
  29.  
  30.  
  31. class TFTGDlg (quarkpy.qmacro.dialogbox):
  32.  
  33.     #
  34.     # dialog layout
  35.     #
  36.  
  37.     size = (350, 260)
  38.     dfsep = 0.4        # separation at 40% between labels and edit boxes
  39.     dlgflags = FWF_KEEPFOCUS
  40.  
  41.     dlgdef = """
  42.         {
  43.             Style = "15"
  44.         Caption = "Tricky Force to Grid"
  45.         sep: = {Typ="S" Txt=" "}
  46.             info: = {Typ="S" Bold="0" Txt="Use this to detect and repair slightly off-the-grid polyhedrons."}
  47.             info: = {Typ="S" Bold="0" Txt="Warning ! This is a bad trick and should only be used to repair"}
  48.             info: = {Typ="S" Bold="0" Txt="maps that are really messed up ! You should make a backup copy"}
  49.             info: = {Typ="S" Bold="0" Txt="of your map before proceeding..."}
  50.         sep: = {Typ="S" Txt=" "}
  51.         epsilon: =
  52.         {
  53.         Txt = "Move not more than"
  54.         Typ = "EF1"
  55.         SelectMe = "1"
  56.         }
  57.             nepsilon: =
  58.             {
  59.                 Txt = "Normal at least"
  60.                 Typ = "EF1"
  61.             }
  62.         sep: = { Typ = "S" Txt=""}
  63.         ok1:py = {Txt="" }
  64.         //ok2:py = {Txt="" }
  65.             cancel:py = {Txt="" }
  66.     }
  67.     """
  68.  
  69.     def __init__(self, form, editor):
  70.         self.editor = editor
  71.         src=quarkx.newobj(":")
  72.         src["epsilon"] = 0.01,
  73.         src["nepsilon"] = 0.99,
  74.         quarkpy.qmacro.dialogbox.__init__(self, form, src,
  75.         ok1 = quarkpy.qtoolbar.button(
  76.                 self.TFTG,
  77.                 "force all faces to match the grid",
  78.                 ico_editor, 2,
  79.                 "Force Faces"),
  80.         #ok2 = quarkpy.qtoolbar.button(
  81.             #    self.TFTG,
  82.             #    "do it",
  83.             #    ico_editor, 2,
  84.             #    "Force Faces"),
  85.         cancel = quarkpy.qtoolbar.button(
  86.             self.close,
  87.             "close this box",
  88.             ico_editor, 0,
  89.             "Cancel"))
  90.  
  91.     def TFTG(self, btn):
  92.         editor = self.editor
  93.         flist = editor.Root.findallsubitems("", ':f')    # all faces
  94.         grid = editor.gridstep
  95.         if not grid:
  96.             grid = 1.0
  97.         quarkx.globalaccept()
  98.         epsilon, = self.src["epsilon"]
  99.         nepsilon, = self.src["nepsilon"]
  100.         def fix(f):
  101.             if f>0:
  102.                 return 1
  103.             else:
  104.                 return -1
  105.         undo = quarkx.action()
  106.         for f in flist:
  107.             if f.broken: continue
  108.            # tp = list(f.threepoints(0))
  109.            # change = 0
  110.            # for i in range(3):
  111.            #     v = list(tp[i].tuple)
  112.            #     for j in range(3):
  113.            #         test = v[j]/grid
  114.            #         if test != int(test):
  115.            #             delta = test-int(test)
  116.            #             if delta>0.5: delta = delta-1
  117.            #             if abs(delta)<epsilon:
  118.            #                 v[j] = int(test+0.5)*grid
  119.            #                 change = 1
  120.            #     tp[i] = quarkx.vect(v[0], v[1], v[2])
  121.            # if change:
  122.            #     new = f.copy()
  123.            #     new.setthreepoints((tp[0], tp[1], tp[2]), 0)
  124.            #     undo.exchange(f, new)
  125.  
  126.             new = None
  127.             n = f.normal
  128.             if not n: continue
  129.             n = n.normalized
  130.             if abs(n.x)==1:
  131.                 dir = 0
  132.             elif abs(n.y)==1:
  133.                 dir = 1
  134.             elif abs(n.z)==1:
  135.                 dir = 2
  136.             elif abs(n.x)>nepsilon:
  137.                 new = f.copy()
  138.                 new.distortion(quarkx.vect(fix(n.x), 0, 0), f.origin)
  139.                 dir = 0
  140.             elif abs(n.y)>nepsilon:
  141.                 new = f.copy()
  142.                 new.distortion(quarkx.vect(0, fix(n.y), 0), f.origin)
  143.                 dir = 1
  144.             elif abs(n.z)>nepsilon:
  145.                 new = f.copy()
  146.                 new.distortion(quarkx.vect(0, 0, fix(n.z)), f.origin)
  147.                 dir = 2
  148.             else:
  149.                 continue
  150.             if new is None:
  151.                 k = f
  152.             else:
  153.                 k = new
  154.             p1, p2, p3 = k.threepoints(0)
  155.             v = p1.tuple
  156.             test = v[dir]/grid
  157.             if test != int(test):
  158.                 delta = test-int(test)
  159.                 if delta>0.5: delta = delta-1
  160.                 if abs(delta)<epsilon:
  161.                     v = [0,0,0]
  162.                     v[dir] = -grid*delta
  163.                     if new is None: new = f.copy()
  164.                     new.translate(quarkx.vect(tuple(v)))
  165.             if new is not None:
  166.                 undo.exchange(f, new)
  167.         editor.ok(undo, "Faces To Grid")
  168.         self.close()
  169.  
  170.  
  171. def TFTGclick (m):
  172.     editor = mapeditor ()
  173.     if editor is None: return
  174.     TFTGDlg(quarkx.clickform, editor)
  175.  
  176.  
  177. #--- add the new menu item into the "Commands" menu ---
  178.  
  179. quarkpy.mapcommands.items.append(quarkpy.qmenu.sep)   # separator
  180. quarkpy.mapcommands.items.append(
  181.   quarkpy.qmenu.item("Tricky force to grid...", TFTGclick, "|Tricky force to grid:\n\nRepairs maps that are off-the-grid.|intro.mapeditor.menu.html#commandsmenu"))
  182.  
  183.  
  184. # ----------- REVISION HISTORY ------------
  185. #
  186. #
  187. # $Log: map2grid.py,v $
  188. # Revision 1.5  2003/09/14 05:57:08  cdunde
  189. # Typ separator correction
  190. #
  191. # Revision 1.4  2003/03/24 08:57:15  cdunde
  192. # To update info and link to infobase
  193. #
  194. # Revision 1.3  2001/06/17 21:10:57  tiglari
  195. # fix button captions
  196. #
  197. # Revision 1.2  2000/06/03 10:25:30  alexander
  198. # added cvs headers
  199. #
  200. #
  201. #
  202. #